var express = require("express");
var router = express.Router();
var projectInfoService = require("../../service/project/projectService")
const authMiddleware = require("../../middleware/authMiddleware");
//列表
router.get(
    "/list",
    async (req, res, next) => {
        let list = await projectInfoService.query(req.query);
        res.json({ code: 1, data: list });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//新增
router.post(
    "/add",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        console.log('req', req.body)
        projectInfoService.add(req.body).then(() => {
            res.json({ code: 1, msg: "新增成功" });
        });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//更新
router.post(
    "/update",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        projectInfoService.update(req.body).then(() => {
            res.json({ code: 1, msg: "更新成功" });
        });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//删除
router.post(
    "/remove",
    authMiddleware.verifyToken,
    async (req, res, next) => {
        let { _id } = req.body;
        if (_id == "" || !_id) return res.json({ code: 0, msg: "_id不存在" });
        projectInfoService.delete({ _id: _id }).then(() => {
            res.json({ code: 1, msg: "删除成功" });
        });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);
//查询
router.get(
    "/model",
    async (req, res, next) => {
        let { _id } = req.query;
        console.log('_id',_id)
        let list = await projectInfoService.query({ _id });
        res.json({ code: 1, data: list.length > 0 ? list[0] : {} });
    },
    (err) => {
        res.json({ code: 0, msg: err });
    }
);

module.exports = router;